package com.hb.trade.server.flow.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.hb.core.pojo.flow.entity.FlowApprover;
import com.hb.core.pojo.flow.entity.FlowMain;
import com.hb.core.pojo.flow.dto.FlowTempMainDTO;
import com.hb.core.pojo.parent.MPBaseController;
import com.hb.core.pojo.parent.PageResult;
import com.hb.trade.server.flow.manager.FlowTempMainManager;
import com.hb.trade.server.flow.service.FlowMainService;
import com.hb.trade.server.flow.service.FlowTempMainService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.util.MsgUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 流程主表 前端控制器
 *
 * @author zhaojk
 * @since 2021-05-27
 */
@RestController
@RequestMapping("/flowMain")
public class FlowMainController extends MPBaseController<FlowMain, FlowMainService> {

    protected final FlowTempMainService flowTempMainService;

    public FlowMainController(FlowMainService service,
                              FlowTempMainService flowTempMainService) {
        super(service);
        this.flowTempMainService = flowTempMainService;
    }

    /**
     * 发起流程
     *
     * @return
     */
    @PostMapping("/launch")
    public Message launch(@RequestBody FlowTempMainDTO flowTempMainDto) {
        String errorMsg = flowTempMainDto.to(FlowTempMainManager.class).launchValid();
        if (StrUtil.isNotEmpty(errorMsg)) {
            return MsgUtil.fail(errorMsg);
        }
        boolean r = service.launchFlow(flowTempMainDto);
        return MsgUtil.msg(r);
    }

    /**
     * 获取指定用户的待办列表
     *
     * @return
     */
    @PostMapping("/todo")
    public Message todo(@RequestBody FlowApprover approver) {
        Long approverId = approver.getApproverId();
        if (approverId == null) {
            return MsgUtil.fail("必须指定用户ID");
        }
        IPage result = service.todoByUserId(approver);
        return MsgUtil.success(PageResult.get(result));
    }
}
